package com.gorkr.cronback.pattern.designPattern.observer;


import com.gorkr.cronback.pattern.CronPattern;
import com.gorkr.cronback.pattern.designPattern.factory.ValueMatcherFactory;
import com.gorkr.cronback.pattern.designPattern.proxy.CronPatternInterface;
import com.gorkr.cronback.pattern.designPattern.proxy.ProxyCronPattern;
import com.gorkr.cronback.pattern.designPattern.singleton.CronPatternUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 解析出最近10次执行时间
 */
public class ParseToTenTimesObserver implements CronObserver{
    private CronStringSubject cronStringSubject;

    public ParseToTenTimesObserver(CronStringSubject cronStringSubject){
        this.cronStringSubject = cronStringSubject;
        cronStringSubject.registerCronObserver(this);
    }


    @Override
    public List update(String cronString) throws Exception {

        GregorianCalendar startCalendar = new GregorianCalendar();

        GregorianCalendar endCalendar = new GregorianCalendar(2030, Calendar.FEBRUARY,1,1,1);

        ValueMatcherFactory valueMatcherFactory = new ValueMatcherFactory();
        CronPatternUtil cronPatternUtil = CronPatternUtil.getInstance();
//        CronPattern cron = new CronPattern(cronString,valueMatcherFactory);
        ProxyCronPattern cron = new ProxyCronPattern(cronString,valueMatcherFactory);

        final List<String> dates = cronPatternUtil.matchedDates(cron,startCalendar.getTimeInMillis(),endCalendar.getTimeInMillis(),10);
        return dates;
    }
}
